<html>
<head><meta charset="utf-8"><title>AST simplification pass · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html">AST simplification pass</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="196992742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/196992742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#196992742">(May 09 2020 at 13:54)</a>:</h4>
<p>Hello! Is there any simplification pass done on the AST before lowering to HIR? I am trying to debug something and i don't understand most of it but would it be possible that an <code>Expr</code> of <code>kind: Closure</code> be transformed in its inner block? Or if you look at differently, that the <code>Closure</code> level is removed and its inner block left instead in the tree?</p>



<a name="197024181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197024181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197024181">(May 10 2020 at 01:00)</a>:</h4>
<p>No simplification.</p>



<a name="197024194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197024194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197024194">(May 10 2020 at 01:01)</a>:</h4>
<p>Besides regular macro expansion and a couple of extra expansion passes like test harness generation, all AST transformations and simplifications are happening during lowering to HIR.</p>



<a name="197024258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197024258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197024258">(May 10 2020 at 01:03)</a>:</h4>
<p>However, some side tables later used from HIR <em>are</em> built as if AST were modified, e.g. <code>DefId</code> hierarchies (<code>src\librustc_resolve\def_collector.rs</code>).</p>



<a name="197047579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197047579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197047579">(May 10 2020 at 10:37)</a>:</h4>
<p>Ok. So its during <code>def_collector</code> that i can see the <code>Expr</code> with <code>kind: Closure</code> and then later during during lowering i can only see the block of the cloure</p>



<a name="197047695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197047695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197047695">(May 10 2020 at 10:39)</a>:</h4>
<p>So during <code>def_collector</code> i can see: main function -&gt; block -&gt; closure expr -&gt; block -&gt; macrodef<br>
But during lowering i can see: main function -&gt; block -&gt; block -&gt; macrodef</p>



<a name="197047734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197047734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197047734">(May 10 2020 at 10:39)</a>:</h4>
<p>Because the closure expr is gone, its not lowered properly and the macrodef (which is properly lowered) has a parent def id that does not have an associated hir id</p>



<a name="197047818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197047818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197047818">(May 10 2020 at 10:41)</a>:</h4>
<p>And i printed the AST at the beginning of the lower_crate function and the closure expr is already gone at this point so something must happen <em>before</em></p>



<a name="197050552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197050552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197050552">(May 10 2020 at 11:41)</a>:</h4>
<p>Hmm, is this happening in rustdoc by chance?</p>



<a name="197050602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197050602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197050602">(May 10 2020 at 11:42)</a>:</h4>
<p>Because rustdoc does have a pass removing parts of AST unnecessary for documentation purposes.</p>



<a name="197053613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197053613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197053613">(May 10 2020 at 12:47)</a>:</h4>
<p>Oh yes its during a rustdoc test</p>



<a name="197053623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197053623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197053623">(May 10 2020 at 12:47)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span>  <code>src/test/rustdoc/macro-in-closure.rs</code></p>



<a name="197053819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197053819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197053819">(May 10 2020 at 12:52)</a>:</h4>
<p>The relevant pass is <code>ReplaceBodyWithLoop</code>.</p>



<a name="197053829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197053829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197053829">(May 10 2020 at 12:53)</a>:</h4>
<p>AFAIK, it was modified in the past to not strip "things with items inside them", perhaps this is a missing case.</p>



<a name="197053860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197053860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197053860">(May 10 2020 at 12:53)</a>:</h4>
<p>thank you very much for the help, i'll look for that and try to understand what is happening!</p>



<a name="197055118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197055118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197055118">(May 10 2020 at 13:22)</a>:</h4>
<p>Something definitely happens in <code>ReplaceBodyWithLoop::visit_block</code></p>



<a name="197081558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197081558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197081558">(May 10 2020 at 22:08)</a>:</h4>
<p>So i managed to do a PR that fixes the issue: <a href="https://github.com/rust-lang/rust/pull/72088" title="https://github.com/rust-lang/rust/pull/72088">https://github.com/rust-lang/rust/pull/72088</a></p>



<a name="197245357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/AST%20simplification%20pass/near/197245357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/AST.20simplification.20pass.html#197245357">(May 12 2020 at 08:19)</a>:</h4>
<p>This is kind of a nightmare because fixing in one place, make other places fail :(</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>